using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Drawing;
using System.Data;
using DevExpress.XtraGrid.Views.Grid;
using THSERP;
using THSLib;
using THSERP.BaseProvider;
using THSERP.BusinessEntities;
using System.Windows.Forms;
namespace THSERP.Modules.POS
{
    public partial class BRPOSLocationsGridControl : THSGridControl
    {
        public BRPOSLocationsGridControl()
        {
            InitializeComponent();
        }

        public BRPOSLocationsGridControl(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
        public override void InitializeControl()
        {
            base.InitializeControl();
            
            //this.UseEmbeddedNavigator = false;
           
        }
        protected override DevExpress.XtraGrid.Views.Grid.GridView InitializeGridView()
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            //gridView.DataSourceChanged += new EventHandler(gridView_DataSourceChanged);
            if (gridView.Columns["FK_GELocationID"] != null)
                gridView.Columns["FK_GELocationID"].VisibleIndex = 0;
            if (gridView.Columns["FK_BRPOSID"] != null)
            {
                gridView.Columns["FK_BRPOSID"].VisibleIndex = 1;
                gridView.Columns["FK_BRPOSID"].Width = 120;
            }
            if(gridView.Columns["BRPOSLocationStatusCombo"]!=null)
                gridView.Columns["BRPOSLocationStatusCombo"].VisibleIndex = 2;
           // gridView.BestFitColumns();
            return gridView;
        }
        
        protected override void THSGridControl_DataSourceChanged(object sender, EventArgs e)
        {
            DataSet dsBRPOSs = new BRPOSsController().GetAllByEmployeeAndDate(THSApp.CurrentUsersInfo.FK_HREmployeeID, THSApp.WorkingDate);
            DevExpress.XtraGrid.GridControl grid = ((POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity).BRPOSLocationList.GridControl;
            if (grid != null)
            {
                DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit reponsitory =
                    (DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit)((POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity).BRPOSLocationList.GridView.Columns["FK_BRPOSID"].ColumnEdit;
                if (dsBRPOSs != null)
                    reponsitory.DataSource = dsBRPOSs.Tables[0];
                DevExpress.XtraGrid.Views.Grid.GridView gridview = (DevExpress.XtraGrid.Views.Grid.GridView)this.Views[0];
                int iRowhandle = gridview.FocusedRowHandle;
                if (iRowhandle >= 0)
                {
                    BRPOSLocationsInfo objBRPOSLocationsInfo = (BRPOSLocationsInfo)gridview.GetRow(iRowhandle);
                    if (objBRPOSLocationsInfo != null)
                    {
                        //((POSModule)Screen.Module).Invalidate(objBRPOSLocationsInfo.FK_BRPOSID);
                        if (objBRPOSLocationsInfo.FK_BRPOSID > 0)
                        {
                            ((POSModule)Screen.Module).Toolbar.CurrentIndex = ((POSModule)Screen.Module).Toolbar.GetCurrentIndex(objBRPOSLocationsInfo.FK_BRPOSID);
                            ((POSModule)Screen.Module).Toolbar.Invalidate();
                        }
                    }
                }
            }
        }
        public override void InitGridControlDataSource()
        {
            //base.InitGridControlDataSource();
            POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            BindingSource bds = new BindingSource();
            bds.DataSource = entity.BRPOSLocationList;
            this.DataSource = bds;
        }
       
        #region GridControl Event Handlers
        protected override void GridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {

            //base.GridView_FocusedRowChanged(sender, e);
            DataSet dsBRPOSs = new BRPOSsController().GetAllByEmployeeAndDate(THSApp.CurrentUsersInfo.FK_HREmployeeID, THSApp.WorkingDate);
            DevExpress.XtraGrid.GridControl grid = ((POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity).BRPOSLocationList.GridControl;
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit reponsitory = 
                (DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit)((POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity).BRPOSLocationList.GridView.Columns["FK_BRPOSID"].ColumnEdit;
            if (dsBRPOSs != null)
                reponsitory.DataSource = dsBRPOSs.Tables[0];
            DevExpress.XtraGrid.Views.Grid.GridView gridview = (DevExpress.XtraGrid.Views.Grid.GridView)this.Views[0];
            int iRowhandle = gridview.FocusedRowHandle;
            if (iRowhandle >= 0)
            {
                BRPOSLocationsInfo objBRPOSLocationsInfo = (BRPOSLocationsInfo)gridview.GetRow(iRowhandle);
                if (objBRPOSLocationsInfo != null)
                {
                    //((POSModule)Screen.Module).Invalidate(objBRPOSLocationsInfo.FK_BRPOSID);
                    if (objBRPOSLocationsInfo.FK_BRPOSID > 0)
                    {
                        ((POSModule)Screen.Module).Toolbar.CurrentIndex = ((POSModule)Screen.Module).Toolbar.GetCurrentIndex(objBRPOSLocationsInfo.FK_BRPOSID);
                        ((POSModule)Screen.Module).Toolbar.Invalidate();
                    }
                }
            }
            //ProposeRemainingAmount();
        }
        protected override void OnClick(EventArgs ev)
        {
            base.OnClick(ev);
            DataSet dsBRPOSs = new BRPOSsController().GetAllByEmployeeAndDate(THSApp.CurrentUsersInfo.FK_HREmployeeID, THSApp.WorkingDate);
            DevExpress.XtraGrid.GridControl grid = ((POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity).BRPOSLocationList.GridControl;
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit reponsitory =
                (DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit)((POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity).BRPOSLocationList.GridView.Columns["FK_BRPOSID"].ColumnEdit;
            if (dsBRPOSs != null)
                reponsitory.DataSource = dsBRPOSs.Tables[0];
            DevExpress.XtraGrid.Views.Grid.GridView gridview = (DevExpress.XtraGrid.Views.Grid.GridView)this.Views[0];
            int iRowhandle = gridview.FocusedRowHandle;
            if (iRowhandle >= 0)
            {
                BRPOSLocationsInfo objBRPOSLocationsInfo = (BRPOSLocationsInfo)gridview.GetRow(iRowhandle);
                if (objBRPOSLocationsInfo != null)
                {
                    //((POSModule)Screen.Module).Invalidate(objBRPOSLocationsInfo.FK_BRPOSID);
                    if (objBRPOSLocationsInfo.FK_BRPOSID > 0)
                    {
                        ((POSModule)Screen.Module).Toolbar.CurrentIndex = ((POSModule)Screen.Module).Toolbar.GetCurrentIndex(objBRPOSLocationsInfo.FK_BRPOSID);
                        ((POSModule)Screen.Module).Toolbar.Invalidate();
                    }
                }
            }
        }
        #endregion

        protected override string GetLookupTableDisplayColumn(string strLookupTableName)
        {
            switch (strLookupTableName)
            {
                case "BRPOSs":
                    return "BRPOSNo";
                case "GELocations":
                    return "GELocationName";
                default:
                    return base.GetLookupTableDisplayColumn(strLookupTableName);
            }
        }
    }
}
